<?php

class ProductStatsController extends BaseController
{
	
	//产品销量统计
	public function actionAdmin()
	{
		$model = new Product();
		$model->unsetAttributes();
		$model->list_order = 2;//销量倒序排列
		$model->min_sale_num = 1;//最小销量
		$model->zp_state = array(1,2);//不为删除
		$model->zp_status = 2;//审核通过
		
		//获取筛选条件
		if(isset($_GET['Product'])){
			$model->attributes = $_GET['Product'];
			if(!empty($_GET['Product']['cateid3'])){
				$model->catgory_id = (int)$_GET['Product']['cateid3'];
			}elseif(!empty($_GET['Product']['cateid2'])){
				$model->catgory_id = (int)$_GET['Product']['cateid2'];
			}else{
				$model->catgory_id = (int)$_GET['Product']['cateid1'];
			}
		}
		
		$this->render('admin',array(
				'model'=>$model
		));
	}

	//数据导出
	public function actionExport(){
		$model = new Product();
		$model->unsetAttributes();
		
		//获取筛选条件
		if(isset($_GET['Product'])){
			$model->attributes = $_GET['Product'];
			if(!empty($_GET['Product']['cateid3'])){
				$model->catgory_id = (int)$_GET['Product']['cateid3'];
			}elseif(!empty($_GET['Product']['cateid2'])){
				$model->catgory_id = (int)$_GET['Product']['cateid2'];
			}else{
				$model->catgory_id = (int)$_GET['Product']['cateid1'];
			}
		}
		
		$criteria=new CDbCriteria;
		//搜索类型搜索
		if(!empty($model->search_type)){
			if($model->search_content=='请输入关键字')
				$model->search_content = '';
			if($model->search_type==1){
				//产品名称
				$model->zp_name = $model->search_content;
			}elseif($this->search_type==2){
				//公司名称
				$criteria->join .= ' LEFT JOIN {{company}} c ON c.zm_id=t.zm_id ';
				$criteria->addCondition("c.zc_name like '%".$model->search_content."%'");
			}
		}
		//行业分类
		if(!empty($model->catgory_id)){
			$criteria->join .= ' LEFT JOIN {{category}} zca ON zca.zca_id=t.zca_id ';
			$criteria->addCondition(" (zca.zca_id={$model->catgory_id} OR zca.zca_parentid={$model->catgory_id} OR zca.zca_topid={$model->catgory_id})");
		}
		//销量倒序排列
		$criteria->order = 'zp_sale_num DESC';
		$criteria->compare('zp_status',2);//审核通过
		$criteria->compare('zp_state',array(1,2));//不过删除
		$products = Product::model()->findAll($criteria);
		$this->toExcel($products,array('zp_id','zm_id','zp_name','zp_price','zp_inventory','zp_sale_num','zp_draft:datetime','zp_hits','zp_fav_num'),'销量排行榜');
		exit;
	}

	
	public function behaviors()
	{
		return array(
				'eexcelview'=>array(
						'class'=>'ext.eexcelview.EExcelBehavior',
				),
		);
	}
}